/**
 * Created by cchen on 2015/6/8.
 */

'use strict';

angular.module('backofficeApp.bannerOrder.controllers', []).
    controller('bannerOrderCtrl', ['$scope', '$routeParams', 'CommunityService', '$http', '$location', function ($scope, $routeParams, communityService, $http, $location) {

        $scope.bannerType = [
            {
                name: 'banner',
                value: 1
            },
            {
                name: '新鲜事',
                value: 2
            }
        ];

        $scope.communityId = $routeParams.communityId;
        $scope.type = Number($routeParams.type);

        $scope.communityLabelList = communityService.getAllCommunityService();

        $scope.getCommunityBanner = function () {

            $location.path('/bannerOrder/' + $scope.type + '/' + $scope.communityId)
        };

        $scope.getData = function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/getCommunityBannerList/' + $scope.type + '/' + $scope.communityId, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.bindList = data.data;
            }).error(function () {
            });
        };

        if ($scope.communityId != null) {
            $scope.getData($scope.communityId);
        }

        $scope.setDisplayOrder = function (bindId) {
            bootbox.prompt('设置顺序（数字越大越靠前）', function (result) {
                if (result != null && result != '') {
                    if (!/^(\+|-)?(\d+)(\.\d*)?$/g.test(result)) {
                        alert('只允许输入数字');
                        return false;
                    }

                    var opt = {
                        bindId: bindId,
                        displayOrder: Number(result)
                    };
                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/banner/setBindBannerOrder', opt, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (result) {
                        $scope.getData($scope.communityId);
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        }
    }]);